
log_format  ad_log '[$time_local] $remote_addr "$arg_event" "$arg_position"'
 ' "$arg_ad" "$arg_account" "$arg_url" "$arg_title" "$arg_referrer" $arg_sh $arg_sw $arg_cd'
 ' "$arg_lang" "$arg_utrace" "$http_user_agent"';

	server {
		listen 80;
		server_name {{analysis_domain_name}};


		location = /1.gif {
		    default_type image/gif;
		    access_log off;
		    access_by_lua "
		        -- 用户跟踪cookie名为__utrace
		        local uid = ngx.var.cookie___utrace
		        if not uid then
		            -- 如果没有则生成一个跟踪cookie，算法为md5(时间戳+IP+客户端信息)
	   	            uid = ngx.md5(ngx.now() .. ngx.var.remote_addr .. ngx.var.http_user_agent)

		        end

		        ngx.header['Set-Cookie'] = {'__utrace=' .. uid .. '; path=/'}
		        if ngx.var.arg_account then
		        	-- 通过subrequest到/i-log记录日志，将参数和用户跟踪cookie带过去
		            ngx.location.capture('/i-log?' .. ngx.var.args .. '&utrace=' .. uid)
		        end
		    ";

		    #此请求不缓存
		    add_header Expires "Fri, 01 Jan 1980 00:00:00 GMT";
		    add_header Pragma "no-cache";
		    add_header Cache-Control "no-cache, max-age=0, must-revalidate";

		    #返回一个1×1的空gif图片
		    empty_gif;
		}

		location /i-log {
		    #内部location，不允许外部直接访问
		    internal;



		    #设置变量，注意需要unescape
				set_unescape_uri $u_event $arg_e;
				set_unescape_uri $u_position $arg_p;
		    set_unescape_uri $u_url $arg_url;
		    set_unescape_uri $u_title $arg_title;
		    set_unescape_uri $u_referrer $arg_referrer;
		    set_unescape_uri $u_sh $arg_sh;
		    set_unescape_uri $u_sw $arg_sw;
		    set_unescape_uri $u_cd $arg_cd;
		    set_unescape_uri $u_lang $arg_lang;
		    set_unescape_uri $u_utrace $arg_utrace;
		    set_unescape_uri $u_account $arg_account;

		    log_subrequest on;

				open_log_file_cache max=1000 inactive=10s;

				access_log {{access_log_path}} ad_log;
				error_log {{error_log_path}} ;

		    #输出空字符串
		    echo '';
		}
	}
